package com.hh.servlet.subject;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hh.dao.SubjectDAO;

@WebServlet("/SubjectSubmit")
public class SubjectSubmit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SubjectSubmit() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		String pdbbm = request.getParameter("pdbbm");
		String pdbmc = request.getParameter("pdbmc");
		String zyhm = (String) request.getSession().getAttribute("zyhm");
		String ygdm = (String) request.getSession().getAttribute("ygdm");
		SubjectDAO dao = new SubjectDAO();
		String pdbm = "J"+System.currentTimeMillis()/10000;
		Map<String,String[]> map = request.getParameterMap();
		Set<String> set = map.keySet();
		Double pdfs = 0d;
		for(String key:set){
			if(!key.equals("pdbbm")&&!key.equals("pdbmc")){
				String value = map.get(key)[0];
				String ysbm = key.split("_")[0];
				String ysmc = key.split("_")[1];
				String yszbm = value.split("_")[0];
				String yszmc = value.split("_")[1];
				Double yszf = Double.parseDouble(value.split("_")[2]);
				pdfs = pdfs + yszf;
				dao.insertSubject(pdbm, ysbm, ysmc, yszbm, yszmc);
			}
		}
		String pdjg = "";
		JSONArray rules = dao.getRules(pdbbm);
		for(int i=0;i<rules.size();i++){
			JSONObject rule = rules.getJSONObject(i);
			int fs = rule.getIntValue("fs");
			int fs1 = rule.getIntValue("fs1");
			
			boolean rule1 = false;
			boolean rule2 = false;
			switch(rule.getIntValue("fh")){
			case 1://>
				if(pdfs>fs){
					rule1=true;
				}
				break;
			case 2://>=
				if(pdfs>=fs){
					rule1=true;
				}
				break;
			case 3://<
				if(pdfs<fs){
					rule1=true;
				}
				break;
			case 4://<=
				if(pdfs<=fs){
					rule1=true;
				}
				break;
			default:
				break;
			
			}
			
			switch(rule.getIntValue("fh1")){
			case 1://>
				if(pdfs>fs1){
					rule2=true;
				}
				break;
			case 2://>=
				if(pdfs>=fs1){
					rule2=true;
				}
				break;
			case 3://<
				if(pdfs<fs1){
					rule2=true;
				}
				break;
			case 4://<=
				if(pdfs<=fs1){
					rule2=true;
				}
				break;
			default:
				break;
			
			}
			
			if(rule.getIntValue("lj")==1){//and
				if(rule1&&rule2){
					pdjg = rule.getString("jg");
				}
			}else{//or
				if(rule1||rule2){
					pdjg = rule.getString("jg");
				}
			}
			
		}
		
		dao.insertAssess(pdbm, pdbbm, pdbmc, zyhm,pdfs,ygdm,pdjg);
		response.sendRedirect("SubjectList?zyhm="+zyhm+"&ygdm="+ygdm);
	}

}
